package slh.myMapReduce;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class FavoriteMovieMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    // 定义常量1，用于计数
    private final static IntWritable one = new IntWritable(1);
    private Text movieId = new Text();

    @Override
    public void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {

        // 将输入行按逗号分割(假设是CSV格式)
        String[] parts = value.toString().split(",");

        // 检查是否为电影类型(CONTENT_TYPE='movie')且有足够字段
        if (parts.length >= 3 && "movie".equals(parts[2].trim())) {
            movieId.set(parts[1].trim()); // CONTENT_ID是电影ID
            context.write(movieId, one);  // 输出<电影ID, 1>
        }
    }
}